package cc.customer.web.login;


import cc.customer.common.utils.Utils;
import cc.customer.system.domain.Dept;
import cc.customer.system.service.DeptService;
import cc.customer.system.service.RoleService;
import cc.customer.system.service.UserRoleService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.customer.authorization.annotation.CurrentUser;
import cc.customer.authorization.manager.RedisTokenManager;
import cc.customer.common.domain.ResponseBo;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;

import javax.servlet.http.HttpServletRequest;


/**
 * 接口安全认证的示例controller
 */
@Controller
@RequestMapping("/app")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);


    @Autowired
    private RedisTokenManager redisTokenManager;


    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    /**
     * 用户登录信息
     */
    @RequestMapping(value = "/login", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo login( @RequestParam String username, @RequestParam String password, @RequestParam String rId) {
        MyUser user = userService.findByName(username);
        String msg = "登录成功！";
        
        if (user == null) {
            logger.info("用户登录 账号错误");
            msg = "账号错误！";
            return ResponseBo.error(msg);
        }

        if (user != null && "0".equals(user.getStatus() )) {
            logger.info("账号已经被锁定");
            msg = "账号已经被锁定！";
            return ResponseBo.error(msg);
        }
        if (user != null && !this.passwordEncoder.matches(password,user.getPassword())) {
            logger.info("用户登录密码错误");
            msg = "用户登录密码错误！";
            return ResponseBo.error(msg);
        }	

        // 生成一个token，保存用户登录状态
        String token = redisTokenManager.createToken(String.valueOf(user.getUserId()));
        if(StringUtils.isNotBlank(rId)){
            user.setrId(rId);
            userService.updateByPrimaryKey(user);
        }
        user.setPassword(null);
        user.setToken(token);
        Dept byId = deptService.findById(user.getDeptId());
        user.setDeptName(byId.getDeptName());
        String role = userRoleService.selectRole(user.getUserId());
        user.setRoleName(role);
        return ResponseBo.ok(msg,user);
    }
    
    /**
     * 获取当前用户的信息
     */
    @RequestMapping(value = "getUser", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo getUser(@CurrentUser MyUser user) {
        return ResponseBo.ok("获取用户成功==="+user.getUsername()+"===="+user.getNickname());
    }
    
    /**
     * 退出登陆方法示例
     */
    @RequestMapping(value = "logout", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo logout(@CurrentUser MyUser user) {
        if (user == null) {
            return ResponseBo.error("退出失败");
        }
        redisTokenManager.deleteToken(user.getToken());
        return ResponseBo.ok("退出成功");
    }

 
}
